public class Demo8 {
    private static boolean flag = true;
    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(()->{
           while(flag){
               System.out.println("hello");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        thread.start();
        Thread.sleep(3000);
        flag = false;
        //在主线程里就可以随时通过 flag 变量的取值, 来操作 t 线程是否结束.
    }
}
